package 设计模式.memento.clone;

import lombok.Data;

/**
 * 发起人自主备份和恢复
 */
@Data
public class Originator implements Cloneable {

    private Originator backup;

    private String state;

    // 创建一个备忘录
    public void createMemento(){
        this.backup = this.clone();
    }

    // 恢复一个备忘录
    public void restoreMemento(){
        if(null != backup){
            this.state = this.backup.state;
        }
    }

    @Override
    protected Originator clone() {
        try{
            return  (Originator) super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }

        return null;
    }
}
